package com.lc.fbtp.draftmsg.core;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

public class Test {
	public static void main(String[] args) {
		RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
		OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
		ThreadMXBean threads = ManagementFactory.getThreadMXBean();
		MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
		ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
		long count = 0;
		while (true) {
			Thread t = new Thread() {

				@Override
				public void run() {
					while (true) {
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}

			};
			t.start();
			count++;
			if (count % 10 == 0) {
				System.out.printf("jvmName:%s %s %s%n", runtime.getVmName(), "version", runtime.getVmVersion());
				System.out.printf("jvmJavaVer:%s%n", System.getProperty("java.version"));
				System.out.printf("jvmVendor:%s%n", runtime.getVmVendor());
				System.out.printf("jvmUptime:%s%n", toDuration(runtime.getUptime()));
				System.out.printf("threadsLive:%d%n", threads.getThreadCount());
				System.out.printf("threadsDaemon:%d%n", threads.getDaemonThreadCount());
				System.out.printf("threadsPeak:%d%n", threads.getPeakThreadCount());
				System.out.printf("threadsTotal:%d%n", threads.getTotalStartedThreadCount());
				System.out.printf("heapCurr:%d%n", mem.getHeapMemoryUsage().getUsed() / Kb);
				System.out.printf("heapMax:%d%n", mem.getHeapMemoryUsage().getMax() / Kb);
				System.out.printf("heapCommitted:%d%n", mem.getHeapMemoryUsage().getCommitted() / Kb);
				System.out.printf("osName:%s %s %s%n", os.getName(), "version", os.getVersion());
				System.out.printf("osArch:%s%n", os.getArch());
				System.out.printf("osCores:%s%n", os.getAvailableProcessors());
				System.out.printf("clsCurrLoaded:%s%n", cl.getLoadedClassCount());
				System.out.printf("clsLoaded:%s%n", cl.getTotalLoadedClassCount());
				System.out.printf("clsUnloaded:%s%n", cl.getUnloadedClassCount());
			}
//			if(count==500l){
//				break;
//			}

		}

	}

	private final static int Kb = 1024;

	private static NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));
	private static NumberFormat fmtD = new DecimalFormat("###,##0.000", new DecimalFormatSymbols(Locale.ENGLISH));

	protected static String toDuration(double uptime) {
		uptime /= 1000;
		if (uptime < 60) {
			return fmtD.format(uptime) + " seconds";
		}
		uptime /= 60;
		if (uptime < 60) {
			long minutes = (long) uptime;
			String s = fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
			return s;
		}
		uptime /= 60;
		if (uptime < 24) {
			long hours = (long) uptime;
			long minutes = (long) ((uptime - hours) * 60);
			String s = fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
			if (minutes != 0) {
				s += " " + fmtI.format(minutes) + (minutes > 1 ? " minutes" : " minute");
			}
			return s;
		}
		uptime /= 24;
		long days = (long) uptime;
		long hours = (long) ((uptime - days) * 24);
		String s = fmtI.format(days) + (days > 1 ? " days" : " day");
		if (hours != 0) {
			s += " " + fmtI.format(hours) + (hours > 1 ? " hours" : " hour");
		}
		return s;
	}

}
